package main

import (
	"flag"
	"fmt"
	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/rest"
	"go.uber.org/zap"
	"img_analyse/analyse/api/internal/config"
	"img_analyse/analyse/api/internal/handler"
	"img_analyse/analyse/api/internal/svc"
	"img_analyse/middleware/Cors"
	logger "img_analyse/middleware/Logger"
	"net/http"
)

var configFile = flag.String("f", "etc/image-api.yaml", "the config file")

func main() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c)

	logger.InitLogger("logs/image_service.log", "info")
	defer logger.Sync()

	server := rest.MustNewServer(c.RestConf)
	defer server.Stop()
	server.Use(Cors.CorsMiddleware)
	server.Use(logger.RequestLogger)
	ctx := svc.NewServiceContext(c)
	handler.RegisterHandlers(server, ctx)

	logger.Logger.Info("Starting server",
		zap.String("host", c.Host),
		zap.Int("port", c.Port),
	)

	// ✅ 静态文件服务：把 /static 映射到本地 rpc/display 目录
	http.Handle("/static/display/", http.StripPrefix("/static/display", http.FileServer(http.Dir("D:\\img_analyse\\img_analyse\\analyse\\rpc\\display"))))

	// ✅ 静态文件服务：把 /static/original 映射到本地 rpc/original 目录
	http.Handle("/static/original/", http.StripPrefix("/static/original", http.FileServer(http.Dir("D:\\img_analyse\\img_analyse\\analyse\\rpc\\original"))))

	// ✅ 启动静态服务（两个不同的端口）
	// display 服务在 8886 端口
	go func() {
		fmt.Println("Serving static files for display at http://localhost:8886/static/display/")
		err := http.ListenAndServe(":8886", nil)
		if err != nil {
			fmt.Println("Error serving static files:", err)
		}
	}()

	// original 服务在 8885 端口
	go func() {
		fmt.Println("Serving static files for original at http://localhost:8885/static/original/")
		err := http.ListenAndServe(":8885", nil)
		if err != nil {
			fmt.Println("Error serving static files:", err)
		}
	}()

	// 启动 Go-zero 的 REST API 服务
	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
	server.Start()
}
